/**
 * @intro: 数据请求统一封装.
 */
//导入模块
import axios from 'axios'
import {port_code} from '../port_url'
import {Message} from 'element-ui'
import {server_base_url} from '../config';

export default function fetch(options) {

    return new Promise((resolve, reject) => {
      // https://github.com/mzabriskie/axios
  
      //创建一个axios实例
      const instance = axios.create({
        //设置默认根地址
        baseURL: server_base_url,
        //设置请求超时设置
        timeout: 2000,
        //设置请求时的header
        headers: {
          'Github-url': 'https://github.com/zzmhot/vue-admin',
          'X-Powered-By': 'zzmhot'
        }
      })
      //请求处理
      instance(options)
        .then(({data: {code, msg, data}}) => {
          //请求成功时,根据业务判断状态
          if (code === port_code.success) {
            resolve({code, msg, data})
            return false
          } else if (code === port_code.unlogin) {
            // setUserInfo(null)
            // router.replace({name: "login"})
          }
          Message.warning(msg)
          reject({code, msg, data})
        })
        .catch((error) => {
          console.log("fetch调用失败")
          //请求失败时,根据业务判断状态
          if (error.response) {
            let resError = error.response
            let resCode = resError.status
            let resMsg = error.message
            Message.error('操作失败！错误原因 ' + resMsg)
            reject({code: resCode, msg: resMsg})
          }
        })
    })
  }

